package com.sec.android.app.myfiles.operation.compress;

import android.content.Context;
import android.util.SparseArray;

import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.operation.FileOperationException;
import com.sec.android.app.myfiles.operation.ProgressListener;

import java.util.ArrayList;

/**
 * Created by daesu83.kim on 2015-07-17.
 */
public class CompressMgr {

    private static SparseArray<CompressMgr> sInstanceArr = new SparseArray<>();

    private AbsCompressorImp mCompressorImp;

    public static CompressMgr getInstance(int processId) {
        CompressMgr instance = sInstanceArr.get(processId);
        if (instance == null) {
            instance = new CompressMgr();
            sInstanceArr.put(processId, instance);
        }
        return instance;
    }

    public boolean compress(Context context, ArrayList<FileRecord> srcList, FileRecord dst, CompressOptions options,
                            ProgressListener countListener, ProgressListener curFileListener, AbsCompressorImp.DuplicatedRecordListener duplicateListener)
            throws FileOperationException {
        boolean ret = false;
        mCompressorImp = CompressorFactory.getSupportedCompressor(context, srcList, dst, options);
        if (mCompressorImp != null) {
            mCompressorImp.setProgressListener(countListener, curFileListener);
            mCompressorImp.setDuplicatedRecordListener(duplicateListener);
            ret = mCompressorImp.compress(srcList, dst);
            mCompressorImp = null;
        }
        return ret;
    }

    public boolean decompress(Context context, ArrayList<FileRecord> srcList, FileRecord dst, CompressOptions options,
                              ProgressListener countListener, ProgressListener curFileListener, AbsCompressorImp.DuplicatedRecordListener duplicateListener)
            throws FileOperationException {
        boolean ret = false;
        mCompressorImp = CompressorFactory.getSupportedCompressor(context, srcList, dst, options);
        if (mCompressorImp != null) {
            mCompressorImp.setProgressListener(countListener, curFileListener);
            mCompressorImp.setDuplicatedRecordListener(duplicateListener);
            // TODO if multiple decompress is supported, implement it here.
            ret = mCompressorImp.decompress(srcList.get(0), dst);
            mCompressorImp = null;
        }
        return ret;
    }

    public boolean decompress(Context context, FileRecord archiveRecord, FileRecord dst, ArrayList<FileRecord> targetDecompressList, CompressOptions options,
                              ProgressListener countListener, ProgressListener curFileListener, AbsCompressorImp.DuplicatedRecordListener duplicateListener)
            throws FileOperationException {
        ArrayList<FileRecord> srcList = new ArrayList<>();
        srcList.add(archiveRecord);
        boolean ret = false;
        mCompressorImp = CompressorFactory.getSupportedCompressor(context, srcList, dst, options);
        if (mCompressorImp != null) {
            mCompressorImp.setProgressListener(countListener, curFileListener);
            mCompressorImp.setDuplicatedRecordListener(duplicateListener);
            ret = mCompressorImp.decompress(archiveRecord, dst, targetDecompressList);
            mCompressorImp = null;
        }
        return ret;
    }

    public AbsCompressorImp.CompressedItem[] list(Context context, FileRecord src) throws FileOperationException {
        AbsCompressorImp.CompressedItem[] result = null;
        CompressOptions options = new CompressOptions();
        options.mCompressMode = CompressOptions.CompressMode.LIST_FILES;
        ArrayList<FileRecord> srcList = new ArrayList<>();
        srcList.add(src);
        mCompressorImp = CompressorFactory.getSupportedCompressor(context, srcList, null, options);
        if (mCompressorImp != null) {
            result = mCompressorImp.list(src);
            mCompressorImp = null;
        }
        return result;
    }

    public void cancel() {
        if (mCompressorImp != null) {
            mCompressorImp.cancel();
        }
    }
}
